package domotica.client;
import java.awt.Dimension;
import java.math.BigDecimal;
import java.math.MathContext;
import java.util.ArrayList;

import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import domotica.houseowner.HouseOwner;

public class EnvironmentThread implements Runnable{

	private HouseOwner houseOwner;
	private JTable jTable;
	
	public EnvironmentThread(JTable jTable, HouseOwner houseOwner){
		this.houseOwner=houseOwner;
		this.jTable=jTable;
		this.jTable.setEnabled(false);
	}
	
	public void run() {
		
		int i;
		
		ArrayList<String> variablesNames = new ArrayList<String>(houseOwner.getEnvironmentVariablesNames());
		ArrayList<Double> variables = new ArrayList<Double>(houseOwner.getEnvironmentVariables());
		Object[][] measures = new Object[variables.size()][2];
		
		i=0;
		
		for(String name : variablesNames){
			
			measures[i][0]=name;
			measures[i][1]=((Math.round(Double.valueOf(variables.get(i))*10)/10));
			i++;
		}
		jTable.setPreferredScrollableViewportSize(new Dimension(500, 70));
        jTable.setFillsViewportHeight(true);
		
		TableModel tableModel= 
			new DefaultTableModel(measures,
					new String[] { "Misura", "Valore" });
		this.jTable.setModel(tableModel);
		
		while(true){
			
			variables = new ArrayList<Double>(houseOwner.getEnvironmentVariables());
			
			i=0;
			
			for(Double variable : variables){
				tableModel.setValueAt((Math.round(Double.valueOf(variable)*10)/10), i, 1);
				i++;
			}
			
			this.jTable.setModel(tableModel);
			
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}

}
